package com.songshuang.myutilsboot.stlf.io;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.songshuang.myutilsboot.stlf.model.Models;
import com.songshuang.myutilsboot.stlf.util.Base64s;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class Jsons {
    private static final ObjectMapper MAPPER = new ObjectMapper()
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(SerializationFeature.INDENT_OUTPUT, true);

    private Jsons() {}

    public static Models.InputParameter parseAnnOrLrOrExpsCfg(String json) throws JsonProcessingException {
        return MAPPER.readValue(json, Models.InputParameter.class);
    }

    public static String wrapResult(String code, String result, String dataRaw) {
        Map<String, Object> root = new HashMap<>();
        root.put("code", code);
        root.put("result", result);
        root.put("data", Base64s.encode(dataRaw == null ? "" : dataRaw));
        try {
            return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(root);
        } catch (JsonProcessingException e) {
            return "{" + "\"code\":\"500\",\"result\":\"serialize error\",\"data\":\"\"}";
        }
    }

    public static String serializeForecast(Models.ForecastInfo fi) {
        try {
            return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(fi);
        } catch (JsonProcessingException e) {
            return "{}";
        }
    }

    public static String serializeModelVersions(double lng, double lat, List<String> versions) {
        Map<String, Object> root = new HashMap<>();
        root.put("pv_nlng", lng);
        root.put("pv_nlat", lat);
        root.put("model_version", versions);
        try {
            return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(root);
        } catch (JsonProcessingException e) {
            return "{}";
        }
    }
}


